//todo问题
/*
  1. hash字符串是怎么生成的
*/

const path = require("path");
const HtmlWebpackPlugin = require("html-webpack-plugin");

module.exports = {
  entry: "./src/index.js",
  output: {
    filename: "bundle.js",
    path: path.resolve(__dirname, "./dist"), //path必须是绝对路径
    clean: true, //清除无关的文件
    assetModuleFilename: "images/[contenthash][ext]", //配置资源模块名及路径,[contenthash]表示根据文件内容自动生成hash字符串,[ext]表示扩展名, //todo问题:为什么我的没有在dist下面生成文件(用webpack-dev-server不行,webpack可以)
  },
  mode: "development", //"development" | "production" | "none"
  plugins: [
    new HtmlWebpackPlugin({
      template: "./index.html",
      filename: "my-webpack.html",
      inject: "body", //script标签插入的位置
    }),
  ],
  devtool: "inline-source-map", //输出的代码格式与开发环境一致
  devServer: {
    static: "./dist", //使用webpack-dev-server(还自带热更新效果)时以该文件目录作为根目录
  },
  module: {
    rules: [
      //asset开头的前4个是webpack内置的资源模块
      {
        test: /\.svg$/,
        type: "asset/resource", //uri形式
        // generator:{
        //   filename:'dist/[contenthash][ext]'//优先级高于assetModuleFilename
        // }
      },
      {
        test: /\.png$/,
        type: "asset/inline", //base64Data URI
        // generator:{
        //   filename:'dist/[contenthash][ext]'//优先级高于assetModuleFilename
        // }
      },
      {
        test: /\.txt$/,
        type: "asset/source", //导出资源源代码
        // generator:{
        //   filename:'dist/[contenthash][ext]'//优先级高于assetModuleFilename
        // }
      },
      {
        test: /\.jpg$/,
        type: "asset", //通用资源,会根据资源大小自动选择类型,可以配置这个临界值,默认是8K
        parser: {
          dataUrlCondition: {
            maxSize: 4 * 1024 * 1024, //大于4M才生成资源,否则生成base64
          },
        },
      },
      {
        test: /\.css$/,
        use: ["style-loader", "css-loader"], //逆序加载
      },
    ],
  },
};
